package it.tcon.xbeedriver;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.rapplogic.xbee.api.AtCommand;
import com.rapplogic.xbee.api.AtCommandResponse;
import com.rapplogic.xbee.api.XBee;
import com.rapplogic.xbee.api.XBeeAddress16;
import com.rapplogic.xbee.api.XBeeAddress64;
import com.rapplogic.xbee.api.XBeeException;
import com.rapplogic.xbee.api.XBeeRequest;
import com.rapplogic.xbee.api.XBeeResponse;
import com.rapplogic.xbee.api.zigbee.ZNetRemoteAtRequest;
import com.rapplogic.xbee.api.zigbee.ZNetRemoteAtResponse;
import com.rapplogic.xbee.util.ByteUtils;

/** 
 * Here are some examples of API usage.  Most of these examples rely on a pre-existing configuration
 * and should not be run blindly.
 * 
 * 
 * Crashed RXTX oops #
# An unexpected error has been detected by HotSpot Virtual Machine:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x10009e69, pid=1820, tid=6480
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_06-b05 mixed mode, sharing)
# Problematic frame:
# C  [rxtxSerial.dll+0x9e69]
#
# An error report file with more information is saved as hs_err_pid1820.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#
 * @author andrew
 *
 */
public class TestMultipleRemoteCommand {

	private final static Logger log = Logger.getLogger(TestMultipleRemoteCommand.class);
	
	private TestMultipleRemoteCommand() throws XBeeException, InterruptedException {
		
		XBee xbee = new XBee();
		
		try {
			xbee.open("/dev/tty.usbserial-A5001pRJ", 9600);			
			
			XBeeAddress64 addr64 = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x0a, 0x33, 0x98);
			XBeeAddress64 addr64_2 = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x0a, 0x33, 0x7e);
			// Start a thread for process the received responses
			Receive rcv = new Receive(xbee);
			
			Thread rcvTh = new Thread(rcv);
			rcvTh.start();
			Thread.currentThread().sleep(1000);

			for(int i = 1; i <= 32; i++){
				//if(i%8 == 0)
				Thread.currentThread().sleep(100);
				xbee.sendAsynchronous(new ZNetRemoteAtRequest(xbee.getNextFrameId(), addr64, XBeeAddress16.ZNET_BROADCAST, false, "SH"));
			}
			Thread.currentThread().sleep(5000);
			xbee.sendAsynchronous(new ZNetRemoteAtRequest(xbee.getNextFrameId(), addr64_2, XBeeAddress16.ZNET_BROADCAST, false, "SH"));

			for(int i = 1; i <= 32; i++){
				Thread.currentThread().sleep(1000);
				xbee.sendAsynchronous(new ZNetRemoteAtRequest(xbee.getNextFrameId(), addr64_2, XBeeAddress16.ZNET_BROADCAST, false, "SH"));
			}
			
			//xbee.getPacketList().clear();
			
			//Thread.currentThread().sleep(60000);
			
		} finally {
			//xbee.close();
		}
	}
	public static void main(String[] args) throws XBeeException, InterruptedException {
		BasicConfigurator.configure(); 
		new TestMultipleRemoteCommand();
	}
	
	
	private class Receive implements Runnable{
		XBee xbee;
		XBeeResponse response;
		public Receive(XBee xbee){
			this.xbee = xbee;
		}
		public void run(){
			while(true){
				try {
					//xbee.getPacketList().clear();
					this.response = xbee.getResponse();
					
					if (this.response.getApiId() == XBeeResponse.ZNET_REMOTE_AT_RESPONSE) {
						ZNetRemoteAtResponse remote = (ZNetRemoteAtResponse) this.response;
						if(ByteUtils.toBase16(remote.getRemoteAddress64().getAddress()).compareTo("0x00 0x13 0xa2 0x00 0x40 0x0a 0x42 0x9c") == 0){
							log.debug("ERROR");
							xbee.sendAsynchronous(new ZNetRemoteAtRequest(xbee.getNextFrameId(), XBeeAddress64.BROADCAST, XBeeAddress16.ZNET_BROADCAST, false, remote.getCommandName()));
							
						}
						else
						log.debug("status is " + remote);
					}
				} catch (XBeeException e) {
					e.printStackTrace();
				} 
			}
		}
	}
}
